package letcode.problem.dynamicProgra;

public class MinimumPathSum {

    public static int minimumPathSum(int[][] grid) {

        int[][] gridSum = new int[grid.length][grid[0].length];

        int[] firstSet = grid[0];
        for (int i = 1; i < firstSet.length; i++) {
            for (int j = 0; j <= i; j++) {
                gridSum[0][i] += grid[0][j];
            }
        }
        int y = grid.length;
        for (int i = 0; i < y; i++) {
            for (int j = 0; j <= i; j++) {
                gridSum[i][0] += grid[j][0];
            }
        }

        for (int i = 1; i < grid.length; i++) {
            for (int j = 1; j < grid[0].length; j++) {
                gridSum[i][j] = Math.min(gridSum[i - 1][j], gridSum[i][j - 1]) + grid[i][j];
            }
        }
        return gridSum[grid.length - 1][grid[0].length - 1];
    }

    public static void main(String[] args) {
        System.out.println(
                minimumPathSum(
                        new int[][]{
                                {1,2},
                                {5,6},
                                {1,1}
                        }
                )
        );

    }
}
